package com.ubis.ibms.plugin.dialogs;


import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.ubis.ibms.plugin.dao.CompanyManager;
import com.ubis.ibms.plugin.model.Company;

public class CompanyChooseDialog extends Dialog {
	/*名称筛选框*/
	private Text nameText;
	
	private ListViewer listViewer;
	private List list;
	
	private Company company;
	
	private Button okButton;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public CompanyChooseDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.CLOSE | SWT.TITLE);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//修改name filter内容时查询
		nameText.addModifyListener(new ModifyListener(){
			public void modifyText(ModifyEvent arg0) {
				java.util.List<Company> companies = CompanyManager.findByName(nameText.getText().trim());
				listViewer.setInput(companies);
				System.out.println("nameText modified.");
			}
		});
		
		listViewer = new ListViewer(container, SWT.BORDER | SWT.V_SCROLL);
		list = listViewer.getList();
		list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		listViewer.setContentProvider(ArrayContentProvider.getInstance());
		listViewer.setLabelProvider(new LabelProvider() {
			public String getText(Object element) {
				if (element instanceof Company) {
					Company company = (Company) element;
					return company.getName();
				}
				return super.getText(element);
			}
		});
		//双击选中公司
		listViewer.addDoubleClickListener(new IDoubleClickListener(){
			public void doubleClick(DoubleClickEvent event) {
				//返回选中行的company
				IStructuredSelection selection = (IStructuredSelection)event.getSelection();
				company = (Company)selection.getFirstElement();
				close();
			}
		});
		java.util.List<Company> companies = CompanyManager.getAllCompanies();
		listViewer.setInput(companies);
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		okButton = createButton(parent, IDialogConstants.OK_ID, "确定",
				true);
		//点击确定时选中公司
		okButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				//返回选中行的company
				IStructuredSelection selection = (IStructuredSelection)listViewer.getSelection();
				company = (Company)selection.getFirstElement();
			}
		});
	}
	
	public Company getCompany(){
		return company;
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(300, 450);
	}

	public static void main(String[] args) {
		new CompanyChooseDialog(new Shell()).open();
	}
}
